package com.forlove.app.manager;

import android.content.Context;

import com.forlove.app.ForloveApplication;
import com.forlove.app.common.utils.AppPreferenceHelper;
import com.forlove.app.qselect.login.entity.DataInfoEntity;
import com.forlove.app.qselect.login.entity.EmotionEntity;
import com.forlove.app.qselect.login.entity.GpsEntity;
import com.forlove.app.qselect.login.entity.GroupEntity;
import com.forlove.app.qselect.login.entity.ProvinceEntity;
import com.forlove.app.qselect.login.entity.SchoolInfoEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 所有的页面数据缓存放在此类中
 * Created by yjl on 2017/11/1.
 */

public class DataManager {

    public static void saveFriendCircleInfo(Context context, String friendCircleInfoEntities) {
        AppPreferenceHelper.getInstance(context).putString(PreferenceKeys.CACHE_DATA_FIREND_CIRCLEINFO,
                friendCircleInfoEntities);
    }

    public static String getFriendCircleCache(Context context) {
        return AppPreferenceHelper.getInstance(context).getString(PreferenceKeys.CACHE_DATA_FIREND_CIRCLEINFO, "");
    }


    public static void saveMineCircleInfo(Context context, String friendCircleInfoEntities) {
        AppPreferenceHelper.getInstance(context).putString(PreferenceKeys.CACHE_DATA_MINE_CIRCLEINFO,
                friendCircleInfoEntities);
    }

    public static String getMineCircleCache(Context context) {
        return AppPreferenceHelper.getInstance(context).getString(PreferenceKeys.CACHE_DATA_MINE_CIRCLEINFO, "");
    }


    public static void saveLocaition(Context context, GpsEntity gpsEntity) {
        AppPreferenceHelper.getInstance(context).putString("gps", gpsEntity.serializeToString());

    }

    public static GpsEntity getLocation(Context context) {
        GpsEntity gpsEntity = new GpsEntity();
        String gpsString = AppPreferenceHelper.getInstance(context).getString("gps", "");
        gpsEntity.parseJSONString(gpsString);
        return gpsEntity;
    }

    /**
     * 解析情感状态
     *
     * @param emotion
     * @return
     */
    public static List<EmotionEntity> parseEmotion(Map<String, DataInfoEntity> emotion) {
        List<EmotionEntity> emotionEntities = new ArrayList<>();
        if (emotion == null) {
            return emotionEntities;
        }
        ArrayList<DataInfoEntity> parentEmotion = new ArrayList<>();
        ArrayList<DataInfoEntity> childEmotion = new ArrayList<>();
        for (String key : emotion.keySet()) {
            DataInfoEntity childemotion = emotion.get(key);
            if (childemotion.parentid == 0) {
                parentEmotion.add(childemotion);
            } else {
                childEmotion.add(childemotion);
            }
        }

        for (DataInfoEntity dataInfoEntity : parentEmotion) {
            EmotionEntity emotionEntity = new EmotionEntity();
            emotionEntity.emotion = dataInfoEntity;
            for (DataInfoEntity child : childEmotion) {
                if (dataInfoEntity.id == child.getParentid()) {
                    emotionEntity.dataInfoEntities.add(child);
                }
            }
            emotionEntities.add(emotionEntity);
        }
        return emotionEntities;
    }

    /**
     * 解析城市数据
     *
     * @param mCitys
     * @return
     */
    public static List<ProvinceEntity> parseCityData(Map<String, DataInfoEntity> mCitys) {
        List<ProvinceEntity> provinceEntities = new ArrayList<>();
        if (mCitys == null) {
            return provinceEntities;
        }
        ArrayList<DataInfoEntity> province = new ArrayList<>();
        ArrayList<DataInfoEntity> cityEntities = new ArrayList<>();
        for (String key : mCitys.keySet()) {
            DataInfoEntity cityEntity = mCitys.get(key);
            if (cityEntity.parentid == 0) {
                province.add(cityEntity);
            } else {
                cityEntities.add(cityEntity);
            }
        }

        for (DataInfoEntity proCity : province) {
            ProvinceEntity provinceEntity = new ProvinceEntity();
            provinceEntity.province = proCity;
            for (int i = 0; i < cityEntities.size(); i++) {
                DataInfoEntity cityEntity = cityEntities.get(i);
                if (cityEntity.parentid == proCity.id) {
                    provinceEntity.cityEntities.add(cityEntity);
                }
            }
            provinceEntities.add(provinceEntity);
        }
        return provinceEntities;
    }

    /**
     * 解析年级状态
     *
     * @param groups
     * @return
     */
    public static List<GroupEntity> parseGroupData(Map<String, DataInfoEntity> groups) {
        List<GroupEntity> groupEntities = new ArrayList<>();
        if (groups == null) {
            return groupEntities;
        }
        ArrayList<DataInfoEntity> parenGroup = new ArrayList<>();
        ArrayList<DataInfoEntity> childGroup = new ArrayList<>();
        for (String key : groups.keySet()) {
            DataInfoEntity group = groups.get(key);
            if (group.parentid == 0) {
                parenGroup.add(group);
            } else {
                childGroup.add(group);
            }
        }
        for (DataInfoEntity parentG : parenGroup) {
            GroupEntity groupEntity = new GroupEntity();
            groupEntity.mGroup = parentG;
            for (DataInfoEntity childG : childGroup) {
                if (childG.parentid == parentG.id) {
                    groupEntity.childTag.add(childG);
                }
            }
            groupEntities.add(groupEntity);
        }
        return groupEntities;
    }


    public synchronized static void saveSchoolInfo(String school) {
        AppPreferenceHelper.getInstance(ForloveApplication.getInstance())
                .putString(PreferenceKeys.KEY_SCHOOL_INFO, school);
    }

    public synchronized static String getSchoolInfo() {
        return AppPreferenceHelper.getInstance(ForloveApplication.getInstance())
                .getString(PreferenceKeys.KEY_SCHOOL_INFO, "");
    }

    public static List<SchoolInfoEntity> parseSchool(Map<Integer, SchoolInfoEntity> schoolMap) {
        List<SchoolInfoEntity> schoolInfoEntities = new ArrayList<>();
        for (Integer key : schoolMap.keySet()) {
            SchoolInfoEntity schoolInfoEntity = schoolMap.get(key);
            schoolInfoEntities.add(schoolInfoEntity);
        }
        return schoolInfoEntities;
    }
}
